﻿using System.Linq;
using System.Runtime.Serialization;

namespace Ping.Core
{
	[DataContract]
	public sealed class TracerouteResult
		: Result
	{
		/// <summary>
		/// The array of results, one per hop.
		/// The first is the first hop and the last the target.
		/// </summary>
		[DataMember] public PingResult[] Hops;

		/// <summary>
		/// Whether or not this is a partial result.
		/// A partial result has some servers that didn't respond (for whatever reason).
		/// </summary>
		public bool IsPartial
		{
			get
			{
				return (from r in Hops
				        where r.Sender == null
				        select r).Count() > 0;
			}
		}

		/// <summary>
		/// The total number of hops of the trace.
		/// </summary>
		public int Count
		{
			get { return Hops != null ? Hops.Length : 0; }
		}

		/// <summary>
		/// The number of servers that responded.
		/// </summary>
		public int ValidResultCount
		{
			get
			{
				return (from r in Hops
				        where r.Sender != null
				        select r).Count();
			}
		}

		public int IndexOf(PingResult result)
		{
			for (int i = 0; i < Hops.Length; ++i)
			{
				if (Hops[i] == result)
					return i;
			}

			return -1;
		}
	}
}